luci-app-snmpd: add snmp_version and ip_protocol
authorChristian Korber <[email protected]>
Mon, 29 Jul 2024 07:39:43 +0000 (09:39 +0200)
committerPaul Donald <[email protected]>
Wed, 3 Sep 2025 18:55:05 +0000 (20:55 +0200)
This commit introduces the variables snmp_version and ip_protocol.
These are used in future commits to determine which SNMP version
(snmp_version) to use and which port (ip_protocol) is suitable.

Signed-off-by: Christian Korber <[email protected]>
applications/luci-app-snmpd/htdocs/luci-static/resources/view/snmpd/snmpd.js

index 7a65b847308bda5454d1b245076504390c3e70d4..5d8f6478f5ae83e9be119533be73f4e72159be21 100644 (file)
@@ -2,11 +2,27 @@
 // Karl Palsson <[email protected]> 2021
 'use strict';
 'require form';
+'require uci';
+'require fs';
+'require rpc';
 'require ui';
 'require view';
 
 return L.view.extend({
-       render: function() {
+       load: function() {
+               return Promise.all([
+                       uci.load('snmpd'),
+               ]);
+       },
+
+       __init__: function() {
+               this.super('__init__', arguments);
+
+               this.ip_protocol = null;
+               this.snmp_version = null;
+       },
+
+       render: function(data) {
                let m, s, o, g, go;
 
                m = new form.Map('snmpd',
@@ -44,9 +60,55 @@ return L.view.extend({
                go.default = '0';
                go.rmempty = false;
 
+               this.ip_protocol = g.option(form.ListValue, 'ip_protocol', _('IP version'));
+               this.ip_protocol.value('ipv4', _('Only IPv4'));
+               this.ip_protocol.value('ipv6', _('Only IPv6'));
+               this.ip_protocol.value('ipv4/ipv6', _('IPv4 and IPv6'));
+               this.ip_protocol.optional = false;
+               this.ip_protocol.forcewrite = true;
+               this.ip_protocol.default = 'ipv4';
+               this.ip_protocol.rmempty = false;
+
+               this.ip_protocol.cfgvalue = function(section_id) {
+                       let ip_protocol = uci.get('snmpd', section_id, 'ip_protocol');
+
+                       if (!ip_protocol) {
+                               const s = uci.get_first('snmpd', 'agent');
+                               if (!s)
+                                       return null;
+
+                               const rawAddr = uci.get('snmpd', s['.name'], 'agentaddress');
+                               if (!rawAddr)
+                                       return null;
+
+                               const addr = rawAddr.toUpperCase();
+                               const p = [];
+
+                               if (addr.match(/UDP:\d+/g))
+                                       p.push('ipv4');
+
+                               if (addr.match(/UDP6:\d+/g))
+                                       p.push('ipv6');
+
+                               ip_protocol = p.join('/');
+                       }
+
+                       return ip_protocol;
+               };
+
                go = g.option(form.Value, 'agentaddress', _('The address the agent should listen on'),
                        _('Eg: UDP:161, or UDP:10.5.4.3:161 to only listen on a given interface'));
 
+               this.snmp_version = g.option(form.ListValue, 'snmp_version',
+                       _('SNMP version'),
+                       _('SNMP version used to monitor and control the device'));
+               this.snmp_version.default = 'v1/v2c';
+               this.snmp_version.rmempty = false;
+               this.snmp_version.forcewrite = true;
+               this.snmp_version.value('v1/v2c', _('SNMPv1 and SNMPv2c'));
+               this.snmp_version.value('v1/v2c/v3', _('SNMPv1, SNMPv2c and SNMPv3'));
+               this.snmp_version.value('v3', _('Only SNMPv3'));
+
                go = g.option(form.Value,  'agentxsocket', _('The address the agent should allow AgentX connections to'),
                        _('This is only necessary if you have subagents using the agentX '
                        + 'socket protocol. Eg: /var/run/agentx.sock'));